package com.starblink.android.basic.ext

import android.content.Context
import android.graphics.Color
import android.text.TextUtils
import android.view.Gravity
import android.view.View
import android.widget.TextView
import android.widget.Toast
import androidx.cardview.widget.CardView
import com.starblink.android.basic.R


/**
 * 吐司工具类-还是自己的布局还是自己的icon
 */
 object ToastBussUtils {

    private const val LENGTH_SHORT = Toast.LENGTH_SHORT

    fun makeText(context: Context?, str: String?) {
        if (null == context || TextUtils.isEmpty(str)) {
            return
        }

        val view = View.inflate(context, R.layout.custom_toast_normal_view, null)
        val toast = Toast(context)
        toast.view = view
        val tvContent = view.findViewById<View>(R.id.tv_content) as TextView
        tvContent.text = str
        toast.duration = LENGTH_SHORT
        toast.show()

    }

    fun makeText(context: Context?, resID: Int = 0) {
        if (null == context) {
            return
        }
        val view = View.inflate(context, R.layout.custom_toast_normal_view, null)
        val toast = Toast(context)
        toast.view = view
        val tvContent = view.findViewById<View>(R.id.tv_content) as TextView
        tvContent.setText(resID)
        toast.duration = LENGTH_SHORT
        toast.show()


    }

    fun showSuccessText(context: Context?, resID: Int = 0) {
        if (null == context) {
            return
        }

        showSuccessText(context, string(resID))
    }

    fun showSuccessText(context: Context?, msg: String?) {
        if (null == context) {
            return
        }
        msg?:return
        val view = makeCenterToastView(context)
        val toast = Toast(context)
        view.setBackgroundResource(R.drawable.bg_success_toast)
        toast.view = view
        val tvContent = view.findViewById<View>(R.id.tv_content) as TextView
        tvContent.text = msg
        toast.duration = LENGTH_SHORT
        toast.setGravity(Gravity.CENTER, 0, 0)
        toast.show()


    }

    fun showFailText(context: Context?, resID: Int = 0) {
        if (null == context) {
            return
        }

        showFailText(context, string(resID))

    }

    fun showFailText(context: Context?, msg: String?, long: Boolean = false) {
        if (null == context) {
            return
        }
        msg?:return

        val view = makeCenterToastView(context)
        val toast = Toast(context)
        toast.view = view
        val tvContent = view.findViewById<TextView>(R.id.tv_content)
        val drawable = context.resources.getDrawable(R.drawable.ic_toast_no)
        drawable.setBounds(0, 0, drawable.intrinsicWidth, drawable.intrinsicHeight)
        tvContent.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null)
        tvContent.text = msg
        toast.duration = if (long) Toast.LENGTH_LONG  else LENGTH_SHORT
        toast.setGravity(Gravity.CENTER, 0, 0)
        toast.show()
    }

    private fun makeCenterToastView(context: Context) = View.inflate(context, R.layout.custom_toast_bg_view, null).apply {
        (this as? CardView)?.let {
            it.setCardBackgroundColor(Color.WHITE)
            it.radius = 8.dp.toFloat()
            it.setContentPadding(12.dp, 12.dp, 12.dp, 12.dp)
            it.cardElevation = 8.dp.toFloat()
            it.maxCardElevation = 24.dp.toFloat()
        }
    }


    fun showWarning(context: Context?, msg: String?, long: Boolean = false) {
        if (null == context) {
            return
        }
        msg?:return
        val view = makeCenterToastView(context)
        val toast = Toast(context)
        toast.view = view
        val tvContent = view.findViewById<TextView>(R.id.tv_content)
        val drawable = context.resources.getDrawable(R.drawable.ic_toast_w)
        drawable.setBounds(0, 0, drawable.intrinsicWidth, drawable.intrinsicHeight)
        tvContent.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null)
        tvContent.text = msg
        toast.duration = if (long) Toast.LENGTH_LONG  else LENGTH_SHORT
        toast.setGravity(Gravity.CENTER, 0, 0)
        toast.show()
    }


    fun showToastCenter(context: Context?, msg: String?) {
        if (null == context) {
            return
        }

        val view = View.inflate(context, R.layout.custom_toast_bg_view, null)
        val toast = Toast(context)
        toast.view = view
        val tvContent = view.findViewById<View>(R.id.tv_content) as TextView
        tvContent.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null)
        tvContent.text = msg
        toast.duration = LENGTH_SHORT
        toast.setGravity(Gravity.CENTER, 0, 0)
        toast.show()

    }

    fun showToastWishListCenter(context: Context?, msg: String?) {
        if (null == context) {
            return
        }

        val view = View.inflate(context, R.layout.custom_toast_wishlist_bg_view, null)
        val toast = Toast(context)
        toast.view = view
        val tvContent = view.findViewById<View>(R.id.tv_content) as TextView
        tvContent.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null)
        tvContent.text = msg
        toast.duration = LENGTH_SHORT
        toast.setGravity(Gravity.CENTER, 0, 0)
        toast.show()

    }

}
